package com.abel.bigwater.dataserver.model.hgd

import com.abel.bigwater.data.BcdString
import com.abel.bigwater.dataserver.model.PacketException
import com.abel.bigwater.dataserver.model.hgd.HgdUdpData.Companion.toHgdDate
import io.netty.buffer.ByteBuf
import io.netty.buffer.Unpooled
import org.joda.time.DateTime
import java.util.*

abstract class HgdaoBase {

    protected var type = "TCP"

    var station: String? = null

    /**
     * @return the temp
     */
    var temp: ByteArray? = null

    /**
     * @return the fmt
     */
    var fmt: String? = null

    /**
     * the length to set
     */
    var length: Int = 0

    /**
     * the station to set
     */
    var stationInt: Int = 0

    /**
     * the startTime to set
     */
    var startTimeStr: BcdString? = null

    var startTime: Date?
        get() = if (startTimeStr == null) null else startTimeStr?.toHgdDate()
        set(value) {
            startTimeStr = HgdUdpData.fromHgdDate(if (value == null) null else DateTime(value))
        }

    /**
     * the count to set
     */
    var count: Short = 0

    /** in minutes.  */
    var period: Short = 0

    /** voltage/100.0D V.  */
    var voltage: Int = 0

    /**
     * the uploadTimes to set
     */
    var uploadTimes: Int = 0

    /**
     * the signal to set
     */
    var signal: Short = 0

    /**
     * @param type
     */
    constructor(type: String) {
        this.type = type
    }


    @Throws(PacketException::class)
    abstract fun parseBuffer(buf: ByteBuf, ignoreChecksum: Boolean): Boolean

    /**
     * 打包为字节流. 如果出错，字节流不变。
     */
    open fun packBuf(buf: ByteBuf) {
        // nothing.
    }

    companion object {
        const val TYPE_TCP = "TCP"
        const val TYPE_UDP = "UDP"
    }

}
